package org.example.serviceEdu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.example.exceptionHandler.GuliException;
import org.example.serviceEdu.entity.EduChapter;
import org.example.serviceEdu.entity.EduVideo;
import org.example.serviceEdu.entity.chapter.ChapterVo;
import org.example.serviceEdu.entity.chapter.VideoVo;
import org.example.serviceEdu.mapper.EduChapterMapper;
import org.example.serviceEdu.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.serviceEdu.service.EduVideoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author lining
 * @since 2023-10-25
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {
    @Autowired
    private EduVideoService videoService;
    public  List<ChapterVo> getChapterVideoByCourseId(String courseId){
//        查章节
        QueryWrapper<EduChapter>  wrapperChapter=new QueryWrapper<>();
        wrapperChapter.eq("course_id",courseId);
        List<EduChapter> eduChapterList=baseMapper.selectList(wrapperChapter);
//        查小节
        QueryWrapper<EduVideo> wrapperVideo=new QueryWrapper<>();
        wrapperVideo.eq("course_id",courseId);
        List<EduVideo> eduVideoList=videoService.list(wrapperVideo);
        List<ChapterVo> finalList=new ArrayList<>();
//        把章节便利出来封装
        for(int i=0;i<eduChapterList.size();i++) {
            EduChapter eduChapter = eduChapterList.get(i);
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(eduChapter, chapterVo);
            finalList.add(chapterVo);

//        把小节遍历出来封装
            List<VideoVo> videoList = new ArrayList<>();
            for (int m = 0; m < eduVideoList.size(); m++) {
                EduVideo eduVideo = eduVideoList.get(m);
                if (eduVideo.getChapterId().equals(eduChapter.getId())) {
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(eduVideo, videoVo);
                    videoList.add(videoVo);


                }
                chapterVo.setChilren(videoList);
            }
        }
        return finalList;
    }
    public boolean deleteChapter(String chapterId){
        QueryWrapper<EduVideo> wrapper=new QueryWrapper<>();
        wrapper.eq("chapter_id", chapterId);
        int count=videoService.count(wrapper);
        if(count>0){throw new GuliException(20001, "不删除");
        }else {
            int result=baseMapper.deleteById(chapterId);
            return  result>0;
        }
    }
//    根据id删除章节
    @Override
 public void removeChapterByCourseId(String courseId){
        QueryWrapper<EduChapter> wrapper=new QueryWrapper<>();
        wrapper.eq("course_id", courseId);
        baseMapper.delete(wrapper);
    }


}
